package com.pkk.penguin.frame.know.java.interview.year2021.atguigu_20210226.bloomfilter;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

/**
 * 布隆过滤器
 *
 * @author peikunkun
 * @version V1.0
 * @date 2021-03-20 10:50
 **/
public class BloomFilterTest {


  @Test
  public void Administrator_21_20210320105558_() {
    System.out.println("欢迎使用单元测试方法【Administrator_21()_20210320105558】");
    System.out.println("此方法测试描述：【】");

    int size = 1000000;
    BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), size);

    for (int i = 0; i < size; i++) {
      bloomFilter.put(i);
    }

    long startTime = System.nanoTime(); // 获取开始时间
    //判断这一百万个数中是否包含29999这个数
    if (bloomFilter.mightContain(29999)) {
      System.out.println("命中了");
    }

    if (bloomFilter.mightContain(9999999)) {
      System.out.println("命中了");
    }

    long endTime = System.nanoTime();   // 获取结束时间
    System.out.println("程序运行时间： " + (endTime - startTime) + "纳秒");
  }


  @Test
  public void Administrator_45_20210320105644_() {
    System.out.println("欢迎使用单元测试方法【Administrator_45()_20210320105644】");
    System.out.println("此方法测试描述：【】");

    int size = 1000000;
    BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), size, 0.01);
    for (int i = 0; i < size; i++) {
      bloomFilter.put(i);
    }

    List<Integer> list = new ArrayList<Integer>(1000);
    // 故意取10000个不在过滤器里的值，看看有多少个会被认为在过滤器里
    for (int i = size + 10000; i < size + 20000; i++) {
      if (bloomFilter.mightContain(i)) {
        list.add(i);
        System.out.println("误判数：" + i + "已经有:" + list.size() + "误判了");
      }

    }

    System.out.println(bloomFilter.mightContain(99999999));
    System.out.println(bloomFilter.put(99999999));
    System.out.println(bloomFilter.put(99999999));

  }


}
